package com.cqeec.waimaidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimaidemo.common.R;
import com.cqeec.waimaidemo.entity.Employee;
import com.cqeec.waimaidemo.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/employee")
public class EmployController {
    @Autowired
    EmployeeService employeeService;
//    登录功能
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
//        System.out.println(employee);
//        接受用户传过来的密码，并且md加密
        String password= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
//        获取用户输入的信息，进行数据查询，
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
//        构造条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
//        将查询的结果保存起来
        Employee employee1=  employeeService.getOne(queryWrapper);
//        判断用户是否存在
        if (employee1==null){
            return R.error("没找到这个用户");
        }
//        判断用户输入密码是否正确
        if (employee1.getPassword().equals(password)){
            return R.error("密码错误");
        }
//        判断用户状态是否正常
        if (employee1.getStatus()==0){
            return R.error("用户被禁用");
        }
        request.getSession().setAttribute("employee1",employee1.getId());
        return R.success(employee1);

    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("注销成功");
    }
//    员工分页模糊查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
//        添加条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
//    添加员工，post请求
    @PostMapping
    public R<String> save(@RequestBody  Employee employee,HttpServletRequest request){
        System.out.println(employee);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long)  request.getSession().getAttribute("employee"));
        employee.setStatus(1);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        System.out.println(employee);
        employeeService.save(employee);
        return R.success("添加成功");
    }
//    url:'/employee/${id}'
//    method:'get'
//    修改—数据回显
//    Long id
//查询一条数据  再返回
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id")Long id){
        Employee byId = employeeService.getById(id);
    return R.success(byId);
    }
//修改员工
//
@PutMapping
public R<String> update(@RequestBody Employee employee){
    employeeService.updateById(employee);
        return R.success("修改成功");
}

}
